from rest_framework import serializers
from users.models import User
class AdminSerislizer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs = {
            'password':{
                'write_only':True
            }
        }
    # 重写父类保存
    def  create(self, validated_data):

        user = super().create(validated_data)
        user.is_staff = True
        # 密码加密
        user.set_password(validated_data['password'])
        user.save()
        return user
    def update(self, instance, validated_data):
        user = super().update(validated_data)
        user.set_password(validated_data['password'])
        user.save()

        return user